package com.lhd.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.lhd.dbpool.PoolUtil;
import lombok.extern.slf4j.Slf4j;


/**
 * 类 {@code Colinfo} 连接数据库查某个表AAA的所有列信息.
 *
 * @author MapLover
 * @since 2020.09
 */
@Slf4j
public class Colinfo {

    private String tableName;
    private String columnName;
    private String comments;
    private String dataType;
    private int dataLength;
    private int dataPrecision;
    private int dataScale;
    private String columnId;
    private String rule;

    public String getTableName() {
        return tableName;
    }

    public String getColumnName() {
        return columnName;
    }

    public String getComments() {
        return comments;
    }

    public String getDataType() {
        return dataType;
    }

    public int getDataLength() {
        return dataLength;
    }

    public int getDataPrecision() {
        return dataPrecision;
    }

    public int getDataScale() {
        return dataScale;
    }

    public String getColumnId() {
        return columnId;
    }

    public String getRule() {
        return rule;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    public void setDataPrecision(int dataPrecision) {
        this.dataPrecision = dataPrecision;
    }

    public void setDataScale(int dataScale) {
        this.dataScale = dataScale;
    }

    public void setColumnId(String columnId) {
        this.columnId = columnId;
    }

    public void setRule(String rule) {
        this.rule = rule;
    }

    /**
     * @param tableName 表名
     * @return 该表的列信息清单
     */
    public static List<Colinfo> getColinfo(String tableName) {
        Connection conn = null;
        PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement
        ResultSet result = null;// 创建一个结果集对象

        Colinfo cinfo;

        List<Colinfo> clist = new ArrayList<>();

        try {
            //1、获取连接对象
            conn = PoolUtil.getConnection();
            //3、创建sql查询语句
            String sql = "select t.table_name,\r\n" +
                    "       t.column_name,\r\n" +
                    "       t.comments,\r\n" +
                    "       t.data_type,\r\n" +
                    "       t.data_length,\r\n" +
                    "       t.data_precision,\r\n" +
                    "       t.data_scale,\r\n" +
                    "       t.rule,\r\n" +
                    "       t.column_id\r\n" +
                    "  from COLLIST t where t.table_name=? order by column_id";

            pre = conn.prepareStatement(sql);// 实例化预编译语句
            pre.setString(1, tableName);// 设置参数，前面的1表示参数的索引，而不是表中列名的索引
            result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数

            //4、执行sql语句并且换回一个查询的结果集

            while (result.next()) {  //循环遍历结果集

                cinfo = new Colinfo();
                cinfo.setTableName(result.getString("TABLE_NAME"));
                cinfo.setColumnName(result.getString("COLUMN_NAME"));
                cinfo.setComments(result.getString("COMMENTS"));
                cinfo.setDataType(result.getString("DATA_TYPE"));
                cinfo.setDataLength(result.getInt("DATA_LENGTH"));
                cinfo.setDataPrecision(result.getInt("DATA_PRECISION"));
                cinfo.setDataScale(result.getInt("DATA_SCALE"));
                cinfo.setRule(result.getString("RULE"));
                clist.add(cinfo);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            PoolUtil.release(conn, pre, result);
        }
        return clist;
    }

    public static void main(String[] args) {

        List<Colinfo> clist = Colinfo.getColinfo("WAA_TUS_DTA_T");
        log.info("" + clist.size());
    }


}
